package com.insta.android.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.insta.android.core.AssocListViewAdapteur;
import com.insta.android.dao.AssocAdapter;
import com.insta.android.dao.MusicAdapter;
import com.insta.android.model.Music;
import com.insta.android.model.MusicAssoc;

public class AssocActivity extends Activity {

	ListView listView;
	String nomPlayList;
	int idPlaylist;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.assoc);
		TextView textView = (TextView) findViewById(R.id.nomplaylist);
		Intent intent = getIntent();
		nomPlayList = intent.getExtras().getString("nomPlayList");
		idPlaylist = intent.getExtras().getInt("idPlaylist");
		listView = (ListView) findViewById(R.id.assoclistview);
		textView.setText(nomPlayList);
		loadMusics();
	}

	public void onClickAddMusicToPlayList(View v) {
		AssocAdapter assocAdapter = new AssocAdapter(this);

		AssocListViewAdapteur assocListViewAdapteur = (AssocListViewAdapteur) listView
				.getAdapter();

		MusicAssoc[] musicAssocs = assocListViewAdapteur.getMusicAssocs();

		for (MusicAssoc musicAssoc : musicAssocs) {
			Log.d("Yahya", "Selection : " + musicAssoc.isSelected() + " ; "
					+ idPlaylist);
			if (musicAssoc.isSelected() && idPlaylist != -1) {
				assocAdapter.insertAssoc(idPlaylist, musicAssoc.get_id());
			}
		}
		finish();
	}

	public void quitter(View v) {
		finish();
	}

	private void loadMusics() {

		MusicAdapter musicAdapter = new MusicAdapter(this);
		AssocListViewAdapteur assocListViewAdapteur = new AssocListViewAdapteur(
				musicAdapter.getMusicAssoc(0), this);
		listView.setAdapter(assocListViewAdapteur);
	}
}
